/**
 * @Title: ConfigConstants.java
 * @Package com.eppear.jpfm.framework.common
 * @Description: 
 * @author JianFeng Zhang(张建锋)
 * @date 2016-12-12 下午11:48:59
 * @version 
 */
package com.eppear.jpfm.framework.common;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName ConfigConstants
 * @Description 系统级配置文件及常量
 * @author JianFeng Zhang(张建锋)
 * @date 2016-12-12 下午11:48:59
 *
 */
public class ConfigConstants {

	private static Logger log = LoggerFactory.getLogger(ConfigConstants.class);
	/**
	 * @Fields prop : 属性文件
	 */
	private static Properties prop;

	/* 电子邮件相关配置 */
	/** @Fields MAIL_STMP : STMP地址 */
	public static String MAIL_STMP = "";
	/** @Fields MAIL_FROM : 邮件发出邮箱 */
	public static String MAIL_FROM = "";
	/**
	 * @Fields MAIL_PASSWORD : 邮箱密码
	 */
	public static String MAIL_PASSWORD = "";
	/**
	 * @Fields MAIL_USERNAME : 邮箱用户名
	 */
	public static String MAIL_USERNAME = "";
	/**
	 * @Fields MAIL_ENCODE : 邮箱字符集
	 */
	public static String MAIL_ENCODE = "";
	/* 短信服务配置 阿里大鱼 */
	/** @Fields SMS_ENABLE : 系统短信开关,默认为关闭*/
	public static Boolean SMS_ENABLE=false;
	
	/** @Fields ALIDAYU_URL : 短信服务地址*/
	public static String ALIDAYU_URL = "";
	
	/** @Fields ALIDAYU_APPKEY : 应用程序KEY,申请获得*/
	public static String ALIDAYU_APPKEY = ""; 
	
	/** @Fields ALIDAYU_SECRET 阿里大鱼 证书号 */
	public static String ALIDAYU_SECRET = ""; 
	
	/** @Fields ALIDAYU_SMSTEMPLATECODE : 短信模板号码*/
	public static String ALIDAYU_SMSTEMPLATECODE = ""; // 
	
	/** @Fields ALIDAYU_SMSFREESIGNNAME : 短信签名*/
	public static String ALIDAYU_SMSFREESIGNNAME = ""; //
		
	/** @Fields FILE_UPLOAD_BASE_PATH : 上传文件根目录*/
	public static String FILE_UPLOAD_BASE_PATH = "";
	
	/** @Fields MAX_UPLOAD_FILE_SIZE : 上传文件的最大限制*/
	public static Long  MAX_UPLOAD_FILE_SIZE = null;
	
	/**
	 * 服务器地址
	 */
	public static String WEBSOCKET_URL ="";
	/**
	 * 客户端ID
	 */
	public static String WEBSOCKET_ID ="";
	/**
	 * 通讯tocken
	 */
	public static String WEBSOCKET_TOKEN ="";
	/**
	 * 签名有效时间(秒)
	 */
	public static String WEBSOCKET_TIMEOUT ="";
	/**
	 * 微信收费员账号
	 */
	public static String WEBSOCKET_OPERID ="";
	/** 
	 *  微信支付证书根目录
	 */
	public static String WXPAY_CERTPATH ="";
	
	/**
	 * 查询分页,数据的起始行
	 */
	public static final Integer PAGE_DATA_START=0;
	/**
	 * 查询分页,每页的数据行数
	 */
	public static final Integer PAGE_DATA_LENGTH=0;
	/**
	 * 字典缓存 key
	 */
	public static final String CACHE_DIC_KEY = "cacheDic";

	/**
	 * 系统参数缓存key
	 */
	public static final String CACHE_SYS_PARAM_KEY = "cacheSysParam";
	public static final String SYS_INNER_ERROR = "系统内部异常";	
	public static final String SYS_CLOSE_ERROR ="系统维护中";
	
	
	private static final String WsServerKey="websocke.serverUrl",
			WsIdKey = "websocket.id",
			WsTokenKey="websocket.token",
			WsTimeoutKey = "websocket.timeout",
			WsOperIdKey="websocket.operId",
			WxPayCertPath="wxpay.certPath";
			
	static {
		prop = new Properties();
		InputStream in = ConfigConstants.class.getResourceAsStream("/config.properties");
		try {
			prop.load(in);
			
			MAIL_STMP = getProperty("mail.smtp");
			MAIL_FROM = getProperty("mail.from");
			MAIL_ENCODE = getProperty("mail.username");
			MAIL_PASSWORD = getProperty("mail.password");
			MAIL_ENCODE = getProperty("mail.encode");
			
			SMS_ENABLE = (getProperty("SMSEnable")!=null?Boolean.valueOf(getProperty("SMSEnable")):false);
			ALIDAYU_URL = getProperty("alidayu.url");
			ALIDAYU_APPKEY = getProperty("alidayu.appkey");
			ALIDAYU_SECRET = getProperty("alidayu.secret");
			ALIDAYU_SMSTEMPLATECODE = getProperty("alidayu.smstemplatecode");		
			ALIDAYU_SMSFREESIGNNAME = getProperty("alidayu.smsFreesignname");
						
			MAX_UPLOAD_FILE_SIZE = Long.valueOf(getProperty("upload.maxsize"));	
			//对于不同平台的参数，在这里进行判断加载
			String os = System.getProperty("os.name");

			if(os.startsWith("Win")){
				FILE_UPLOAD_BASE_PATH = getProperty("upload.basepath.win");
				WXPAY_CERTPATH = getProperty(WxPayCertPath+".win");
			}else{
				FILE_UPLOAD_BASE_PATH = getProperty("upload.basepath.linux");
				WXPAY_CERTPATH = getProperty(WxPayCertPath+".linux");
			}
			//websocket 相关配置
			WEBSOCKET_URL = getProperty(WsServerKey);
			WEBSOCKET_ID = getProperty(WsIdKey);
			WEBSOCKET_TOKEN = getProperty(WsTokenKey);
			WEBSOCKET_TIMEOUT = getProperty(WsTimeoutKey);
			WEBSOCKET_OPERID = getProperty(WsOperIdKey);
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
	}

	/**
	 * 设置wesocket服务器地址
	 * @param Url
	 */
	public static void setWebSocketServerUrl(String Url) {
		setProperty(WsServerKey, Url);
	}
	/**
	 * 设置wesocket通讯令牌
	 * @param value
	 */
	public static void setWebSocketToken(String value) {
		setProperty(WsTokenKey, value);
	}
	/**
	 * 设置微信收费员Id
	 * @param value
	 */
	public static void setWebSocketOperId(String value) {
		setProperty(WsOperIdKey, value);
	}
	
	/**
	 * @Title getProperty
	 * @Description 获取配置文件中的属性值
	 * @param key
	 * @return
	 */
	private static String getProperty(String key) {
		String value = prop.getProperty(key);
		return value != null ? value.trim() : "";
	}
	
	private static void setProperty(String key,String value) {
		
		FileOutputStream os = null;
		prop.setProperty(key, (value != null ? value.trim() : ""));
		String parth = ConfigConstants.class.getResource("/config.properties").getPath();
		log.debug("============>{}",parth);
		 try {
			os = new FileOutputStream(parth);
			
		} catch (FileNotFoundException e) {
			log.error("打开项目属性文件出错!");
			e.printStackTrace();
		}
		 try {
			prop.store(os,"websocket相关配置项");
			os.flush();
			os.close(); 
		} catch (IOException e) {
			log.error("保存项目属性文件出错!");
			e.printStackTrace();
		}
		 
	}
	
}
